<template>
  <div class="roles-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row>
        <el-col :span="24">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <el-table
        :data="rolesList"
        stripe
        border
        style="width: 100%; margin-top: 20px;">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row class="borderbt rowcenter" :class="index1 === 0 ? 'bordertop' : ''" v-for="(item1, index1) in props.row.children" :key="item1.id">
              <el-col :span="5">
                <el-tag closable type="primary">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row class="rowcenter" :class="index2 !== 0 ? 'bordertop' : ''" v-for="(item2, index2) in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag closable type="success">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span=18>
                    <el-tag @close="delRights(props.row, item3.id)" v-for="item3 in item2.children" :key="item3.id" closable type="warning">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称">
        </el-table-column>
        <el-table-column
          prop="roleDesc"
          label="角色描述">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-search">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-search">删除</el-button>
            <el-button type="warning" size="mini" icon="el-icon-search" @click="showRightsDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="分配权限"
      :visible.sync="rightsDialogVisible"
      width="50%">
      <el-tree ref="tree" :default-checked-keys="defaultCheckedKeys" node-key="id" :data="rightsList" :props="defaultProps" show-checkbox default-expand-all></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],
      rightsDialogVisible: false,
      rightsList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      defaultCheckedKeys: [],
      nowRoleId: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      if(res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
        return false
      }
      this.rolesList = res.data
    },
    delRights(info, rightId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`roles/${info.id}/rights/${rightId}`)
        if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

        this.$message({
          type: 'success',
          message: '删除成功!'
        })

        // this.getRolesList()

        info.children = res.data
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })         
      })
    },
    async showRightsDialog(info) {
      this.rightsDialogVisible = true
      this.defaultCheckedKeys = []
      this.nowRoleId = info.id
      
      const { data: res } =  await this.$http.get('rights/tree')
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.rightsList = res.data

      this.setDefaultCheckedKeys(info, this.defaultCheckedKeys)

    },
    setDefaultCheckedKeys(info, arr) {
      if(!info.children) {
        return arr.push(info.id)
      }

      info.children.forEach((item) => {
        this.setDefaultCheckedKeys(item, this.defaultCheckedKeys)
      })
    },
    async setRights() {
      var arr = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]

      var rids = arr.toString()

      const { data: res } = await this.$http.post(`roles/${this.nowRoleId}/rights`, {
        rids
      })

      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.$message.success(res.meta.msg)

      this.rightsDialogVisible = false
    }
   
    

  }
}
</script>

<style lang="less">
  .roles-container{
    .box-card{
      margin-top: 20px;
    }
    .el-tag{
      margin: 7px;
    }
    .borderbt{
      border-bottom: 1px solid #eee;
    }
    .bordertop{
      border-top: 1px solid #eee;
    }
    .rowcenter{
      display: flex;
      align-items: center;
    }
  }
</style>